package com.doge.walknovel.widgets

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.doge.wnpro.R
import com.doge.walknovel.utils.getString

class AlertDialog private constructor(context: Context, theme: Int) : Dialog(context, theme) {
    private class Params {
        lateinit var mContext: Context
        lateinit var tConfirm: TextView
        lateinit var tCancel: TextView
        lateinit var tTitle: TextView
        lateinit var tMessage: TextView
        var canCancel = true
        var autoDismiss = true
        var mTitle: String? = null
        var mMessage: String? = null
        var cancelText: String? = null
        var confirmText: String? = null
        var cancelCallback: DialogInterface.OnClickListener? = null
        var confirmCallback: DialogInterface.OnClickListener? = null
    }

    class Builder(context: Context) {
        private val p = Params()

        init {
            p.mContext = context
        }

        fun setTitle(title: String): Builder {
            p.mTitle = title
            return this
        }

        fun setMessage(message: String): Builder {
            p.mMessage = message
            return this
        }

        fun setCancelable(cc: Boolean): Builder {
            p.canCancel = cc
            return this
        }

        fun setAutoDismiss(ad: Boolean): Builder {
            p.autoDismiss = ad
            return this
        }

        fun setPositiveButton(
            text: String,
            callback: DialogInterface.OnClickListener
        ): Builder {
            p.confirmText = text
            p.confirmCallback = callback
            return this
        }

        fun show() {
            create().show()
        }

        fun setNegativeButton(
            text: String,
            callback: DialogInterface.OnClickListener
        ): Builder {
            p.cancelText = text
            p.cancelCallback = callback
            return this
        }

        fun create(): AlertDialog {
            val dialog = AlertDialog(
                p.mContext,
                R.style.Theme_Light_NoTitle_Dialog
            )

            val dip1 = (p.mContext.resources.displayMetrics.density + 0.5f).toInt()

            val window = dialog.window
            if (window != null) {
                window.decorView.setPadding(dip1 * 37, 0, dip1 * 37, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.attributes = lp
            }

            @SuppressLint("InflateParams") val view = LayoutInflater.from(p.mContext)
                .inflate(R.layout.dialog_alert, null)
            p.tMessage = view.findViewById(R.id.message)
            p.tCancel = view.findViewById(R.id.cancel)
            p.tConfirm = view.findViewById(R.id.confirm)
            p.tTitle = view.findViewById(R.id.title)

            p.tMessage.text = p.mMessage

            val title = if (p.mTitle.isNullOrEmpty()) getString(R.string.tip) else p.mTitle
            p.tTitle.text = title
            p.tTitle.visibility = View.VISIBLE

            if (p.cancelCallback != null) {
                p.tCancel.text = p.cancelText
                p.tCancel.setOnClickListener {
                    if (p.autoDismiss) dialog.dismiss()
                    p.cancelCallback?.onClick(dialog, 0)
                }
                p.tCancel.visibility = View.VISIBLE
            } else {
                p.tCancel.visibility = View.GONE
            }

            if (p.confirmCallback != null) {
                p.tConfirm.text = p.confirmText
                p.tConfirm.setOnClickListener {
                    if (p.autoDismiss) dialog.dismiss()
                    p.confirmCallback?.onClick(dialog, 1)
                }
            } else {
                p.tConfirm.setOnClickListener {
                    dialog.dismiss()
                }
            }

            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }
    }
}